const PROMISE_STATUS_PENDING = "pending";
const PROMISE_STATUS_FULFILLED = "fulfilled";
const PROMISE_STATUS_REJECT = "rejected";

class HSPromise {
    constructor(executor) {
        this.status = PROMISE_STATUS_PENDING;
        this.value = undefined
        this.reason = undefined
        this.onFulfilledFns = []
        this.onRejectedFns = []

        const resolve = (value) => {
            if (this.status === PROMISE_STATUS_PENDING) {
                queueMicrotask(() => {
                    if (this.status !== PROMISE_STATUS_PENDING) return
                    this.status = PROMISE_STATUS_FULFILLED;
                    this.value = value
                    console.log("resolve被调用");
                    // then 传进来的第一个回调函数
                    // this.onFulfilled(this.value)
                    this.onFulfilledFns.forEach(fn => {
                        fn(this.value)
                    })
                })
            }
        };
        const reject = (reason) => {
            if (this.status === PROMISE_STATUS_PENDING) {
                queueMicrotask(() => {
                    if (this.status !== PROMISE_STATUS_PENDING) return
                    this.status = PROMISE_STATUS_REJECT;
                    this.reason = reason
                    console.log("reject被调用");
                    // then 传进来的第二个回调函数
                    // this.onRejected(this.reason)
                    this.onRejectedFns.forEach(fn => {
                        fn(this.reason)
                    })
                })

            }
        };
        executor(resolve, reject);
    }

    then(onFulfilled, onRejected) {
        return new HSPromise((resolve, reject) => {
            //1.如果在then调用的时候，状态已经确定下来
            if (this.status === PROMISE_STATUS_FULFILLED && onFulfilled) {
                try {
                    const value = onFulfilled(this.value)
                    resolve(value)
                } catch (err) {
                    reject(err)
                }

            }
            if (this.status === PROMISE_STATUS_REJECT && onRejected) {
                try {
                    const reason = onRejected(this.reason)
                    resolve(reason)
                } catch (err) {
                    reject(err)
                }
            }

            //将成功回调和失败的回调放到数组中
            if (this.status === PROMISE_STATUS_PENDING) {
                this.onFulfilledFns.push(()=>{
                 try{
                    const value = onFulfilled(this.value)
                    resolve(value)
                 }catch(err){
                    reject(err)
                 }
                })
                this.onRejectedFns.push(()=>{
                    try{
                        const reson =  onRejected(this.reson)
                        resolve(reson)
                    }catch(err){
                        reject(err)
                    }
             
                })
            }
        })
    }
}

const promise = new HSPromise((resolve, reject) => {
    console.log("状态pending");
    resolve(1111);
    reject(2222);
});

promise.then(res => {
        console.log("res1", res)
        return "aaaaa"
    },
    err => {
        console.log("err1:", err)
    }).then(res => {
    console.log("res2", res)
}, err => {
    console.log("err2", err)
})
// promise.then(res => {
//         console.log("res1", res)
//     },
//     err => {
//         console.log("err1:", err)
//     })

// promise.then(res => {
//         console.log("res2", res)
//     },
//     err => {
//         console.log("err2:", err)
//     })